﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.InteropServices;
namespace RadminPassword
{
    public partial class Form1 : Form
    {

         List<Login> login;
        public Form1()
        {
            
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (File.Exists("login.xml"))
            {
                XmlSerializer xml;
                FileStream stream;
                xml = new XmlSerializer(typeof(List<Login>));
                stream = new FileStream("login.xml", FileMode.Open, FileAccess.Read);
                login = (List<Login>)xml.Deserialize(stream);
                stream.Close();
            }
            else
               
            login=new List<Login>();
        
            bindingSource1.DataSource = login;
           
          
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
           Left = Screen.PrimaryScreen.WorkingArea.Width - Width;
           Top  = Screen.PrimaryScreen.WorkingArea.Height - Height;
            Visible = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Hide();
            e.Cancel = true;
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Show();
        }

        private void выйтиToolStripMenuItem_Click(object sender, EventArgs e)
        {
            XmlSerializer xml;
            FileStream stream;
            xml = new XmlSerializer(typeof(List<Login>));
            stream = new FileStream("login.xml", FileMode.Create, FileAccess.Write);
            xml.Serialize(stream, login);
            stream.Close();

            Application.ExitThread();
        }

        private void настройкиToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Show();
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {

        }

        private void bindingNavigatorAddNewItem_Click_1(object sender, EventArgs e)
        {
           
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        { 
             
            login.Add(new Login());


          

        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                login.RemoveAt(dataGridView1.SelectedCells[0].RowIndex);
             
               
                dataGridView1.Update();
                dataGridView1.Refresh();
            }
            catch { };
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            
            foreach (Login l in login)
            {
                IntPtr hwndCalc;
                IntPtr Form = FindWindow(null, "Система безопасности Windows:  "+l.server);
                if (Form == IntPtr.Zero)
                    Form = FindWindow(null, "Система безопасности Radmin: " + l.server);
                if (Form != IntPtr.Zero)
                {
                    int n = 0;
                    hwndCalc = FindWindowEx(Form, IntPtr.Zero, "Edit", null);
                    while (hwndCalc != IntPtr.Zero)
                    {
                        hwndCalc = FindWindowEx(Form, hwndCalc, "Edit", null);
                        n++;
                    }
                    if (n ==3)
                    {
                        hwndCalc = FindWindowEx(Form, IntPtr.Zero, "Edit", null);
                        SendMessage(hwndCalc, 0x000C, IntPtr.Zero, Marshal.StringToHGlobalUni(l.user));
                        hwndCalc = FindWindowEx(Form, hwndCalc, "Edit", null);
                        SendMessage(hwndCalc, 0x000C, IntPtr.Zero, Marshal.StringToHGlobalUni(l.password)); 
                        hwndCalc = FindWindowEx(Form, IntPtr.Zero, "Button", "OK");
                    SendMessage(hwndCalc, 0x0201, IntPtr.Zero, IntPtr.Zero);
                    SendMessage(hwndCalc, 0x0202, IntPtr.Zero, IntPtr.Zero);


                    }
                    if (n == 2)
                    {
                        hwndCalc = FindWindowEx(Form, IntPtr.Zero, "Edit", null);
                        SendMessage(hwndCalc, 0x000C, IntPtr.Zero, Marshal.StringToHGlobalUni(l.user));
                        hwndCalc = FindWindowEx(Form, hwndCalc, "Edit", null);
                        SendMessage(hwndCalc, 0x000C, IntPtr.Zero, Marshal.StringToHGlobalUni(l.password));
                        hwndCalc = FindWindowEx(Form, IntPtr.Zero, "Button", "ОК");
                        SendMessage(hwndCalc, 0x0201, IntPtr.Zero, IntPtr.Zero);
                        SendMessage(hwndCalc, 0x0202, IntPtr.Zero, IntPtr.Zero);

                    }
                    if (n == 1)
                    {
                        hwndCalc = FindWindowEx(Form, IntPtr.Zero, "Edit", null);
                        SendMessage(hwndCalc, 0x000C, IntPtr.Zero, Marshal.StringToHGlobalUni(l.password));
                        hwndCalc = FindWindowEx(Form, IntPtr.Zero, "Button", "ОК");
                        SendMessage(hwndCalc, 0x0201, IntPtr.Zero, IntPtr.Zero);
                        SendMessage(hwndCalc, 0x0202, IntPtr.Zero, IntPtr.Zero);

                    }
                    
                  
                   
                }
            }
          
        }

        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);

        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam); // отправка сообщения

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle); // поиск дочерних окон

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        private void bindingSource1_DataSourceChanged(object sender, EventArgs e)
        {
            
        }

        private void bindingSource1_BindingComplete(object sender, BindingCompleteEventArgs e)
        {
            
        }

        private void dataGridView1_Validated(object sender, EventArgs e)
        {
           
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            XmlSerializer xml;
            FileStream stream;
            xml = new XmlSerializer(typeof(List<Login>));
            stream = new FileStream("login.xml", FileMode.Create, FileAccess.Write);
            xml.Serialize(stream, login);
            stream.Close();
        }
    }
}
